package com.teradata.controller;

import com.teradata.callback.CallBackImpl;
import com.teradata.config.*;
import com.teradata.util.UniqRequestIdGen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @Project: edb
 * @Description:
 * @Version 1.0
 * @Throws :
 * @Author: <li>2019/4/18 lyndon Create 1.0
 * @Copyright (c)2018-2018 中央结算公司，版权所有
 * @Modified By:
 */

@RestController("/task")
public class TaskController {

    public static List<String> taskGrpList = Arrays.asList("TG1","TG2");

    @Autowired
    private AsyncConfiguration asyncConfiguration;

    @GetMapping("/start")
    public String startTask() {
        long startTimeMillis = System.currentTimeMillis();
        String reqId = UniqRequestIdGen.resolveReqId();
        TaskContext taskContext = asyncConfiguration.taskGroupRegist(getRandomTaskGrpName());
        ITask task = new ITaskImpl();
        taskContext.submit(task, new CallBackImpl(task));
        //asyncTaskByThread.submit(reqId, taskContext);
        long endTimeMillis = System.currentTimeMillis();
        System.out.println(reqId + " submit " + (endTimeMillis - startTimeMillis));
        return "startTask";
    }


    public String getRandomTaskGrpName() {
        int i = (int) (1 + Math.random() * 2 - 1);
        return taskGrpList.get(i);
    }

}
